<template>
  <div class="wrap">
    <div class="header">
      <div class="left">
        <img
          :src="info.customer_avatar?info.customer_avatar:require('../assets/user.png')"
          :onerror="defaultImg"
          class="headerUrl"
          alt
        />
        <div class="center">
          <div>
            <p>{{info.customer_name}}</p>
            <!-- <span style="display:block;">{{info.customer_tel}}</span>  -->
            <a
              style="display:block;color:#719bef"
              :href="'tel:'+info.customer_tel"
            >{{info.customer_tel}}</a>

            <span>订单数：{{info.customer_order_count}}</span>
          </div>
        </div>
      </div>
      <span style="display:block;color:#999;font-size: .426667rem;">客户</span>
    </div>
    <div class="dateBox">
      <div class="lkbox">
        <div class="lk" @click="orderState = true">
          <p :class="sendStateTag==1?'act':''">{{sendStateName}}</p>
          <img :src="sendStateTag==1?require('../assets/top2.png'):require('../assets/zx.png')" alt />
        </div>
        <div class="lk" @click="show = true">
          <p :class="createUserIdState==1?'act':''">{{createUserIdName}}</p>
          <img
            :src="createUserIdState==1?require('../assets/top2.png'):require('../assets/zx.png')"
            alt
          />
        </div>
      </div>
      <div class="date" @click="dateState = true">
        <p>{{date?date:"日期筛选"}}</p>
        <img :src="date?require('../assets/top.png'):require('../assets/bot.png')" alt />
      </div>
    </div>
    <div class="centers">
      <div class="item" v-for="(item,idx) in productList" :key="idx">
        <div class="item-left">
          <div class="remake">
            <img
              :src="item.order_image"
              @click="showMaxImg(item.order_image)"
              :onerror="defaultImg2"
              class="avi"
              alt
            />
            <p>{{item.remark?item.remark:'无备注'}}</p>
          </div>
          <div class="productInfo">
            <div class="box1">
              <p @click="link(item,1)" style="display:flex;align-items: center;">
                客户：
                <i>{{item.customer_name}}</i>
              </p>
              <p>规格：{{item.order_size}}</p>
              <p>价格：￥{{item.customer_price }}</p>
            </div>
            <p class="edit" @click="editramke(item)">编辑</p>
          </div>
        </div>
        <div class="item-right">
          <div class="box2">
            <div class="state">
              <p :style="item.send_state==0?'':'color:green'">{{item.send_state==0?'未到货':'已到货'}}</p>
              <img
                src="../assets/select.png"
                v-if="item.radio"
                @click="select(idx)"
                class="radio"
                alt
              />
              <img
                src="../assets/radio.png"
                v-if="!item.radio"
                @click="select(idx)"
                class="radio"
                alt
              />
            </div>
            <p @click="link(item,2)">
              供应商:
              <i>{{item.supplier_name}}</i>
            </p>
            <p>
              录入人:
              <i style="color:#999">{{item.create_user_name}}</i>
            </p>
          </div>
          <span>{{item.proxy_state==0?'暂无代发':'客户已提交代发'}}</span>
        </div>
      </div>
      <van-empty description="暂无数据" v-if="state1==1" />
    </div>
    <div class="footer">
      <div>
        <p class="btn" @click="setallState">修改订单状态</p>
      </div>
      <div @click="selectAll">
        <p>全选</p>
        <img src="../assets/select.png" v-if="selectState" class="all" alt />
        <img src="../assets/radio.png" v-if="!selectState" class="all" alt />
      </div>
    </div>
    <van-action-sheet
      v-model="show"
      :actions="actions"
      description="选择录入人"
      :round="false"
      @select="onSelect"
    />
    <van-action-sheet
      v-model="orderState"
      description="选择订单状态"
      :round="false"
      :actions="orderData"
      @select="onOrder"
    />
    <van-action-sheet
      v-model="allState"
      :round="false"
      description="修改订单状态"
      :actions="EfitorderData"
      @select="editState"
    />
    <van-popup v-model="dateState" position="bottom">
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        @confirm="dateConfirm"
        @cancel="dateCancel"
         cancel-button-text="全部"
      />
    </van-popup>
    <van-popup v-model="remakeState" position="top" :style="{ height: '150px' }">
      <van-field v-model="sms" center clearable label="备注:" class="inp" placeholder="请输入备注">
        <van-button slot="button" class="bj" size="small" @click="setRemake" type="primary">确定</van-button>
      </van-field>
    </van-popup>
    <van-popup v-model="showMax">
      <img :src="showMaxSrc" class="maxImg" :onerror="defaultImg2" alt srcset />
    </van-popup>
  </div>
</template>
<style lang="scss" scoped>
.maxImg {
  width: 100%;
}
.wrap {
  background: #f7f7f7;
  width: 100%;
  height: 100vh;
}
.inp {
  border-bottom: 1px solid #f3f3f3;
  margin-top: 50px;
  .bj {
    background: #719bef;
    border: 1px solid #719bef;
  }
}
.header {
  height: 2.2rem;
  padding: 0 0.4rem;
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;

  .left {
    display: flex;
    align-items: center;
  }
  .headerUrl {
    width: 1.466667rem;
    height: 1.466667rem;
    border-radius: 50%;
    display: block;
  }
  .center {
    margin-left: 0.4rem;
    div {
      text-align: left;
      p {
        color: #555;
        font-size: 0.48rem;
        text-align: left;
      }
      a {
        color: #999;
        font-size: 0.293333rem;
        text-align: left;
        display: block;
      }
      span:nth-of-type(2) {
        font-size: 0.426667rem;
      }
    }
    p {
      color: #555;
      font-size: 0.266667rem;
      text-align: left;
    }
  }
  .func {
    margin-top: 15px;
    p:nth-of-type(1) {
      width: 2.24rem;
      height: 0.6rem;
      border: 2px solid rgba(63, 217, 201, 1);
      color: #719bef;
      border-radius: 0.4rem;
      font-size: 0.373333rem;
      text-align: center;
      line-height: 0.6rem;
      margin-bottom: 0.266667rem;
    }
    p:nth-of-type(2) {
      width: 2.24rem;
      height: 0.6rem;
      background: rgba(63, 217, 201, 1);
      color: #fff;
      border-radius: 0.4rem;
      font-size: 0.373333rem;
      text-align: center;
      line-height: 0.6rem;
    }
  }
}
.conf {
  display: flex;
  justify-content: flex-end;
  p {
    width: 2.5rem;
    height: 1rem;
    border-radius: 0.266667rem;
    text-align: center;
    background: #719bef;
    font-size: 0.426667rem;
    line-height: 1rem;
    margin-top: 8px;
    color: #fff;
    margin-right: 0.4rem;
  }
}
.dateBox {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.4rem;
  margin: 0.4rem auto 0;
  background: #fff;
  .date {
    padding: 0 0.5rem;
    height: 0.613333rem;
    background: #719bef;
    border-radius: 0.306667rem;
    margin-right: 0.4rem;
    display: flex;
    justify-content: space-around;
    font-size: 0.32rem;
    align-items: center;
    color: #fff;
    p:nth-of-type(2) {
      margin-left: 0.5rem;
    }
    img {
      width: 10px;
      margin-left: 4px;
    }
  }
  .lkbox {
    display: flex;
  }
  .lk {
    display: flex;
    align-items: center;
    margin-left: 0.4rem;
    img {
      width: 10px;
      margin-left: 4px;
      margin-top: 2px;
    }
    p {
      font-size: 0.32rem;
      color: #999;
    }
    .act {
      color: #719bef;
    }
  }
}
.centers {
  position: absolute;
  top: 4rem;
  bottom: 1.2rem;
  left: 0;
  right: 0;
  overflow-y: scroll;
  .item {
    width: 9.466667rem;
    border-radius: 0.266667rem;
    background: #fff;
    margin: 6px auto;
    overflow: hidden;
    display: flex;
    justify-content: space-between;
    padding: 0.2rem 0;
    .item-left {
      display: flex;
    }
    .remake {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      p {
        color: #999;
        font-size: 0.3rem;
        margin-top: 15px;
        margin-left: 0.133333rem;
        width: 2rem;
        text-align: center;
      }
    }
    .avi {
      width: 2rem;
      height: 2rem;
      border-radius: 0.266667rem;
      margin: 0 0.306667rem 0 0.133333rem;
      display: block;
      object-fit: cover;
    }
    .productInfo {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      .box1 {
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        height: 2rem;
      }
      p {
        color: #555;
        font-size: 0.32rem;
        text-align: left;
        i {
          font-style: normal;
          color: #719bef;
          font-size: 0.4rem;
          font-style: normal;
          color: #719bef;
          font-size: 0.4rem;
          white-space: nowrap;
          text-overflow: ellipsis;
          overflow: hidden;
          word-break: break-all;
          width: 2rem;
          display: block;
        }
      }

      .edit {
        width: 1.5rem;
        height: 0.5rem;
        border-radius: 0.266667rem;
        text-align: center;
        background: #719bef;
        font-size: 0.32rem;
        line-height: 0.5rem;
        margin-top: 14px;
        color: #fff;
      }
    }
    .item-right {
      margin-right: 0.8rem;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      .box2 {
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        height: 2rem;
        p {
          color: #555;
          font-size: 0.32rem;
          text-align: left;
          display: flex;
          align-items: center;
          i {
            color: #719bef;
            font-style: normal;
            display: block;
            width: 70px;
            white-space: nowrap;
            text-overflow: ellipsis;
            overflow: hidden;
            word-break: break-all;
            margin-left: 4px;
          }
        }
      }
      .state {
        display: flex;
        justify-content: space-between;
        p:nth-of-type(1) {
          color: #ff536b;
          font-size: 0.426667rem;
          text-align: center;
          margin-right: 0.4rem;
        }
        .radio {
          width: 0.453333rem;
          height: 0.453333rem;
          border-radius: 50%;
          display: block;
          margin-right: 0.3rem;
        }
      }
    }
    span {
      color: #999;
      font-size: 0.32rem;
      display: block;
      margin-top: 15px;
      text-align: left;
    }
  }
}
.footer {
  background: #fff;
  position: fixed;
  bottom: 0;
  height: 1.2rem;
  width: 100%;
  left: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  div {
    display: flex;
    align-items: center;
  }
  .btn {
    background: #719bef;
    color: #fff;
    padding: 0 10px;
    height: 0.8rem;
    border-radius: 0.373333rem;
    font-size: 0.32rem;
    margin-left: 0.733333rem;
    margin-right: 0.266667rem;
    line-height: 0.8rem;
  }
  .all {
    width: 0.453333rem;
    height: 0.453333rem;
    border-radius: 50%;
    display: block;
    margin-right: 0.533333rem;
    margin-left: 0.533333rem;
  }
}
.van-action-sheet {
  max-height: 50%;
  overflow-y: scroll;
}
</style>
<script>
import {
  orderList,
  editBatch,
  remakeEdit,
  getCreateUserOption
} from "@/utils/interface";
import { formatDate } from "@/utils/tools";
import { Toast } from "vant";
import { Empty } from "vant";

export default {
  components: {
    Empty
  },
  data() {
    return {
      show: false,
      orderState: false,
      actions: [],
      orderData: [
        { name: "全部", value: "" },
        { name: "已到货", value: 1 },
        { name: "未到货", value: 0 }
      ],
      EfitorderData: [
        { name: "已到货", value: 1 },
        { name: "未到货", value: 0 },
        { name: "转隔天", value: 2 }
      ],
      currentDate: new Date(),
      dateState: false,
      remakeState: false,
      sms: "",
      productList: [],
      selectState: false,
      allState: false,
      info: {},
      sendStateValue: "",
      date: "",
      remakeId: "",
      user_id: "",
      defaultImg: 'this.src="' + require("../assets/user.png") + '"',
      defaultImg2: 'this.src="' + require("../assets/timg.jpg") + '"',
      showMax: false,
      showMaxSrc: "",
      sendStateName: "所有状态",
      sendStateTag: 0,
      createUserIdName: "所有录入人",
      createUserIdState: 0,
      state1: 0
    };
  },
  created() {
    let date = sessionStorage.getItem("index-date");
    date ? (this.date = date) : "";
    document.title = "订单列表";
    this.user_id = this.$route.query.user_id
    this.createUserIdName =  this.$route.query.createUserIdName
    this.sendStateName = this.$route.query.state==1?'已到货': this.$route.query.state=='0'?'未到货':'所有状态'
    this.info = JSON.parse(this.$route.query.info);
    this.sendStateValue = this.$route.query.state
    console.log(this.info);
    this.getList(
      this.info.customer_tel,
      this.sendStateValue,
      this.date,
      this.user_id,
      this.info.customer_name
    );
    this.getCreateUserOption();
  },
  methods: {
    showMaxImg(s) {
      this.showMaxSrc = s;
      this.showMax = true;
    },
    next() {
      this.$router.push("/submitOrder");
    },
    onSelect(item) {
      console.log(item);
      this.createUserIdName = item.name == "全部" ? "所有录入人" : item.name;
      this.user_id = item.value;
      console.log(this.user_id);
      this.getList(
        this.info.customer_tel,
        this.sendStateValue,
        this.date,
        this.user_id,
        this.info.customer_name
      );

      this.show = false;
    },
    link(data, n) {
      if (n == 1) {
        // 客户
        this.$router.push({
          path: "orderInfo",
          query: { info: JSON.stringify(data) }
        });
      } else {
        // 供应商
        this.$router.push({
          path: "order",
          query: { info: JSON.stringify(data) }
        });
      }
    },
    onOrder(item) {
      console.log(item);
      this.sendStateName = item.name == "全部" ? "所有状态" : item.name;
      this.sendStateValue = item.value;
      this.getList(
        this.info.customer_tel,
        this.sendStateValue,
        this.date,
        this.user_id,
        this.info.customer_name
      );
      this.orderState = false;
    },
    // 修改订单你状态
    editState(item) {
      console.log(item);
      let state = "";
      let day = "";
      let ids = [];
      if (item.value == 2) {
        day = 1;
      } else {
        state = item.value;
      }
      this.productList.map(item => {
        if (item.radio) {
          ids.push(item.order_id);
        }
      });

      editBatch({ sendState: state.toString(), toNextDay: day, ids: ids.toString() }).then(
        res => {
          if (res.data.code == 200) {
            Toast.success("修改成功");
            this.getList(
              this.info.customer_tel,
              this.sendStateValue,
              this.date,
              this.user_id,
              this.info.customer_name
            );
          }
        }
      );
      this.allState = false;
    },
    setallState() {
      let ids = [];
      this.productList.map(item => {
        if (item.radio) {
          ids.push(item.order_id);
        }
      });

      if (ids.length == 0) {
        Toast.fail("选择要操作的订单");

        return false;
      }
      this.allState = true;
    },
    dateConfirm(e) {
      console.log(e);
      this.date = formatDate(e);
      sessionStorage.setItem("index-date", this.date);
      this.getList(
        this.info.customer_tel,
        this.sendStateValue,
        this.date,
        this.user_id,
        this.info.customer_name
      );
      this.dateState = false;
    },
    dateCancel() {
      sessionStorage.setItem("index-date", "");
      this.date = "";
      this.getList(
        this.info.customer_tel,
        this.sendStateValue,
        this.date,
        this.user_id,
        this.info.customer_name
      );
      this.dateState = false;
    },
    // 选中
    select(idx) {
      this.productList[idx].radio = !this.productList[idx].radio;
    },
    // 全选
    selectAll() {
      this.selectState = !this.selectState;
      this.selectState
        ? this.productList.map(item => {
            item.radio = true;
          })
        : this.productList.map(item => {
            item.radio = false;
          });
    },
    getList(customerTel, sendState, createDate, createUserId, customer_name) {
      console.log(sendState);
      if (sendState || sendState === 0) {
        this.sendStateTag = 1;
      } else {
        this.sendStateTag = 0;
      }
      if (createUserId || createUserId === 0) {
        this.createUserIdState = 1;
      } else {
        this.createUserIdState = 0;
      }
      let sendState2 = "";
      console.log(sendState);

      sendState2 = sendState >= 0 ? String(sendState) : "";
      orderList({
        customerTel,
        sendState: sendState2,
        createDate,
        createUserId,
        customerName: customer_name
      }).then(res => {
        if (res.data.data.length == 0) {
          this.state1 = 1;
        } else {
          this.state1 = 0;
        }

        res.data.data.map(item => {
          item.radio = false;
        });
        this.productList = res.data.data;
      });
    },
    editramke(item) {
      this.remakeState = true;
      this.remakeId = item.order_id;
    },
    setRemake() {
      this.remakeState = false;
      if (this.sms) {
        remakeEdit({ orderId: this.remakeId, remark: this.sms }).then(res => {
          if (res.data.code == 200) {
            Toast.success("修改成功");
            this.sms = "";
            this.getList(
              this.info.customer_tel,
              this.sendStateValue,
              this.date,
              this.user_id
            );
          }
        });
      }
    },
    // 获取创建人下拉列表
    getCreateUserOption() {
      getCreateUserOption().then(res => {
        let arr = [];
        res.data.data.map(item => {
          arr.push({ name: item.username, value: item.user_id });
        });
        this.actions = arr;
      });
    }
  }
};
</script>